package cn.tedu.straw.api.user.controller;


import cn.tedu.straw.api.user.dto.StudentRegisterDTO;
import cn.tedu.straw.api.user.ex.InvalidParameterException;
import cn.tedu.straw.api.user.service.IUserService;
import cn.tedu.straw.commons.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-11-11
 */
@RestController
@RequestMapping("/portal/user")
@Slf4j
public class UserController {

    @Autowired
    IUserService userService;

    // http://localhost:8081/portal/user/register/student?inviteCode=JSD2007-666666&phone=13100131001&password=1234&nickName=Alex
    @RequestMapping("/register/student")
    public R<Void> registerStudent(@Valid StudentRegisterDTO studentRegisterDTO,
                                   BindingResult bindingResult) {
        log.debug("StudentRegisterDTO >>> {}", studentRegisterDTO);

        if (bindingResult.hasErrors()) {
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            throw new InvalidParameterException(errorMessage);
        }

        userService.registerStudent(studentRegisterDTO);
        return R.ok();
    }

}
